#
#    Copyright (C) 2003-2005  James Tauber
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

import re



class WikiFormatter:


    def __init__(self, link_prefix):
        self.link_prefix = link_prefix
        

    def __sub_link(self, matchobj):
        wiki_word = matchobj.group(1)
        wiki_word = re.sub(r'\\(.)', '\\1', wiki_word)
        link = self.link_prefix + re.sub(' ', '_', wiki_word).lower()
        return """<a href="%s">%s</a>""" % (link, wiki_word)


    def extract_title(self, page_content):
        m = re.match(r'(?m)^==([^=]+)==', page_content)
        if m:
            return m.group(1)
        else:
            return None

    def __insert(self, resource_id, resource_getter):
        resource = resource_getter(resource_id)
        if resource.exists():
            if hasattr(resource, "get_html"):
                return resource.get_html()
            else:
                return """<p class="debug">invalid insert : %s</p>""" % resource_id
        else:
            return """<p class="debug">unknown insert : %s</p>""" % resource_id # @@@ templatize
    
    def format(self, page_content, leo_file=None, resource_getter=None):
    
        s = page_content
        s = re.sub(r'&', '&amp;', s)
        s = re.sub(r'<', '&lt;', s)
        s = re.sub(r'>', '&gt;', s)
        s = re.sub(r'"', '&quot;', s)
    
        # ==...== to h2
        s = re.sub(r'(?m)^==([^=]+)==', '<h2>\\1</h2>', s)
    
        # ===...=== to h3
        s = re.sub(r'(?m)^===([^=]+)===', '<h3>\\1</h3>', s)
    
        # ====...==== to h4
        s = re.sub(r'(?m)^====([^=]+)====', '<h4>\\1</h4>', s)
    
        # * ... to list
        s = re.sub(r'(?m)^\* (.+)$', '<li>\\1</li>', s)
    
        # ": " at start of line to blockquote 
        s = re.sub(r'(?m)^: (.+)$', '<blockquote>\\1</blockquote>', s)
        s = re.sub(r'</blockquote>(\s*)<blockquote>', '\\1', s)      
    
        # four spaces at start of line to pre
        s = re.sub(r'(?m)^    (.+)$', '<pre>\\1</pre>', s)
        s = re.sub(r'</pre>(\s*)<pre>', '\\1', s)
    
        # *...* bold
        s = re.sub(r'\*([^\*]+)\*', '<b>\\1</b>', s)
    
        # ''...'' italic
        s = re.sub(r"''([^']+)''", '<i>\\1</i>', s)
    
        # --- to emdash
        s = re.sub(r'---', '&#151;', s)
    
        # empty lines -> paragraph boundaries
        s = re.sub(r'(?m)^\s*$', '</p>\n<p>', s)
    
        s = "<p>" + s + "</p>"
    
        s = re.sub(r'<p>\s*<li>', '<ul><li>', s)
        s = re.sub(r'</li>\s*</p>', '</li></ul>', s)
    
        s = re.sub(r'<p>\s*<h2>', '<h2>', s)
        s = re.sub(r'</h2>\s*</p>', '</h2>', s)
    
        s = re.sub(r'<p>\s*<h3>', '<h3>', s)
        s = re.sub(r'</h3>\s*</p>', '</h3>', s)
    
        s = re.sub(r'<p>\s*<h4>', '<h4>', s)
        s = re.sub(r'</h4>\s*</p>', '</h4>', s)
    
        s = re.sub(r'<p></p>', '', s)
    
        URL = r'((?:ftp|http|https):[^ \]]+)'
    
        # [http:..] to external link
        s = re.sub(r'\['+URL+r'\]', '<a class="external" href="\\1">\\1</a>', s)
    
        # [http:... ...] to titled and entitled external link
        s = re.sub(r'\['+URL+r' "([^"]+)" ([^\]]+)\]',
                   '<a class="external" title="\\2" href="\\1">\\3</a>', s)
    
        # [http:... ...] to titled external link
        s = re.sub(r'\['+URL+r' ([^\]]+)\]', '<a class="external" href="\\1">\\2</a>', s)
    
        # [:...] to local link
        s = re.sub(r'\[:/([^ \]]+)\]', '<a href="%s\\1">%s\\1</a>' % (self.link_prefix, self.link_prefix), s)
        s = re.sub(r'\[:([^ \]]+)\]', '<a href="\\1">\\1</a>', s)

        # [:... ...] to titled local link
        s = re.sub(r'\[:/([^ \]]+) ([^\]]+)\]', '<a href="%s\\1">\\2</a>' % self.link_prefix, s)
        s = re.sub(r'\[:([^ \]]+) ([^\]]+)\]', '<a href="\\1">\\2</a>', s)
    
        # [image:... ...] to local image
        s = re.sub('\[image:/([^ \]]+) ([^\]]+)\]', '<img src="%s\\1" alt="\\2"/>' % self.link_prefix, s)    
        s = re.sub('\[image:([^ \]]+) ([^\]]+)\]', '<img src="\\1" alt="\\2"/>', s)    
    
        # [insert:...] to insert a resource
        s = re.sub(r'\[insert:([^\]]+)\]', lambda x: self.__insert(x.group(1), resource_getter), s)
    
        # [amazon:... ...] to link to Amazon using ISBN and link title
        s = re.sub(r'\[amazon:([0-9X]+) ([^\]]+)\]',
                   '<a href="http://www.amazon.com/exec/obidos/tg/detail/-/\\1">\\2</a>', s)
 
        # [..] to wiki link
        s = re.sub(r'(?<!\\)\[(([^\\\]]|(\\\])|(\\(?!\])))+)\]', self.__sub_link, s)

        # remove \ if used to escape [
        s = re.sub(r'(?<!\\)\\\[', r'[', s)
           
        return s
